package com.hna.eking.AirlineServer.security;


import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.hna.eking.AirlineServer.Mapper.CustInfo;
import com.hna.eking.AirlineServer.Mapper.CustInfoMapper;

@Service
public class CustomUserService{
	
	Logger logger = LoggerFactory.getLogger(CustomUserService.class);

	@Autowired
	CustInfoMapper userMapper;
	
	//通过username和uuid查询用户
	public UserDetails loadUserByUsernameAndUUid(String username, String uuid) throws UsernameNotFoundException {
		// TODO Auto-generated method stub
		logger.info("查询用户:{},{}", username, uuid);
		CustInfo custInfo = userMapper.selectByMobileNoAndUuid(username, uuid);
		if (custInfo == null) {
			throw new UsernameNotFoundException("username and uuid is not find in db");
		}
		List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
		authorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
		DeviceUser user = new DeviceUser(custInfo.getCustNo(),custInfo.getMobileNo(), 
				 custInfo.getUUID(), custInfo.getPassword(), authorities, true, true, true);
		
		return user;
	}
	
}